package com.sensePlusVR.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.sensePlusVR.adapter.FollowFollowAdapter;
import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.ActorManager;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.UserUtil;
import com.sensePlusVR.widget.FullyLinearLayoutManager;

@ContentView(R.layout.fragment_follow_follow)
public class FollowFollowFragment extends BaseFragment{
	private static final String TAG = FollowFollowFragment.class.getSimpleName();
	private final String KEY=this.getClass().getSimpleName();
	private DataCenter dataCenter;
	private List<ActorItem> followFollowList;
	private FollowFollowAdapter followFollowAdapter;
	
	@ViewInject(R.id.follow_follow_recycler)
	private RecyclerView follow_follow_recycler;

	@ViewInject(R.id.follow_follow_empty_linear)
	private LinearLayout follow_follow_empty_linear;

	private View rootView;
	
	@SuppressWarnings("unchecked")
	EventListener listenerActors = new EventListener(){
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "listenerActors event : " + event);
			if (event == ActorManager.EventActorIdsGot) {
				List<ActorItem> list = (List<ActorItem>) data;
				if (list != null && list.size() > 0) {
					followFollowList.clear();
					followFollowList.addAll(list);
					followFollowAdapter.refreshData();
				}
			}
		}
		
	};
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dataCenter = DataCenter.getInstans();
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = x.view().inject(this, inflater, container);
			beforeView();
			initView();
		}
		return rootView;
	}
	
	
	private void beforeView() {
		int userId = UserUtil.getUserId();
		if (followFollowList == null) {
			followFollowList = new ArrayList<ActorItem>();
		}
		followFollowList.clear();
		if (followFollowList.size() == 0){
			List<ActorItem> list = dataCenter.getFollowedActors(userId);
			if (list != null){
				followFollowList.addAll(list);
				MyLog.d(TAG, "followFollowList : " + followFollowList.toString());
				Collections.reverse(followFollowList);
				MyLog.d(TAG, "followFollowList reverse : " + followFollowList.toString());
			}
		}
		followFollowAdapter = new FollowFollowAdapter(getActivity(),followFollowList);
		
		
	}

	private void setFollowEmptyUI(int acListSize){

		if(acListSize > 0){

			follow_follow_empty_linear.setVisibility(View.GONE);

		}else{

			follow_follow_empty_linear.setVisibility(View.VISIBLE);

		}
	}
	
	private void initView() {
		FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(getActivity());
		linearLayoutManager.setSmoothScrollbarEnabled(true);
		linearLayoutManager.setAutoMeasureEnabled(true);
		follow_follow_recycler.setLayoutManager(linearLayoutManager);
		follow_follow_recycler.setAdapter(followFollowAdapter);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		dataCenter.addActorEventListener(listenerActors, this, KEY);
		setFollowEmptyUI(followFollowList.size());
		if (followFollowAdapter != null) {
			followFollowAdapter.onResume();
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		dataCenter.delActorEventListener(KEY);
		if (followFollowAdapter != null) {
			followFollowAdapter.onPause();
		}
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if (rootView != null) {
			((ViewGroup) rootView.getParent()).removeView(rootView);
		}
	}

}
